﻿using PBO.Data.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace PBO.Data.SQLite.DAL
{
    public class DALMoveLearnData : DALModel<MoveLearnData>
    {

        #region Instance

        private DALMoveLearnData() { }

        private static DALMoveLearnData instance;

        public static DALMoveLearnData GetInstance()
        {
            if (instance == null)
                instance = new DALMoveLearnData();
            return instance;
        }

        #endregion

        #region funtion

        public MoveLearnData GetWithoutTrait(PokemonCustomInfo info)
        {
            return GetWithoutTrait(info.Identity, info.Ability, info.Moves.Where(m => m != 0).Distinct().ToArray());
        }

        /// <summary>
        /// 验证 技能,特性 共存 如果不能共存返回一个不能共存的技能
        /// </summary>
        public MoveLearnData GetWithoutTrait(int pid, Trait ability, params int[] moves)
        {
            var sb = new StringBuilder();
            sb.Append("select * from pbo_pm_learn");
            sb.Append(" where 1=1");
            sb.Append(" and pid=" + pid);
            sb.Append(" and WithoutTraitId=" + (int)ability);
            if (moves != null && moves.Length > 0)
            {
                sb.AppendFormat(" and ({0})", FindMoveMerger(moves));
            }
            sb.Append(" limit 1");
            string sql = sb.ToString();
            return GetSingle(sql);
        }

        public MoveLearnData GetMove(int pid, int moveId)
        {
            return GetMoves(pid, moveId).FirstOrDefault();
        }

        public List<MoveLearnData> GetMoves(int pid, params int[] moves)
        {
            string mstr = string.Join(",", moves.Select(i => i.ToString()).ToArray());
            string where = "pid=" + pid + " and moveid in (" + mstr + ")";
            return GetModel(where);
        }

        public List<MoveLearnData> GetInheritMoves(int pid, params int[] moves)
        {
            string mstr = string.Join(",", moves.Select(i => i.ToString()).ToArray());
            string where = "pid=" + pid + " and moveid in (" + mstr + ") and LearnType=2";
            return GetModel(where);
        }

        public List<MoveLearnData> GetByPid(int pid)
        {
            return base.GetModel("pid=" + pid);
        }

        /// <summary>
        /// 获取指定蛋组 同时拥有n个不同指定技能的pm
        /// </summary>
        public IEnumerable<int> GetLearnPid(EggGroup eggGroup, int[] exceptGroups, params int[] moves)
        {
            moves = moves.Distinct().ToArray();
            var sb = new StringBuilder();
            sb.Append("select id from ");
            sb.Append("(select l.pid id,count(*) count ");
            sb.Append("from pbo_pm_learn l ");
            sb.Append("left join pbo_pm p ");
            sb.Append("on p.pmid=l.pid ");
            sb.Append("where 1=1 ");
            sb.Append("and p.GenderRestriction<2 ");

            if (eggGroup != EggGroup.无)
            {
                sb.AppendFormat("and (p.egggroup1={0} or p.egggroup2={0}) ", (int)eggGroup);
            }
            if (exceptGroups != null && exceptGroups.Length > 0)
            {
                string groups = string.Join(",", exceptGroups.Distinct().Select(i => i.ToString()).ToArray());
                sb.AppendFormat("and p.egggroup1 not in ({0}) ", groups);
                sb.AppendFormat("and p.egggroup2 not in ({0}) ", groups);
            }
            if (moves != null && moves.Length > 0)
            {
                sb.AppendFormat("and ({0}) ", FindMoveMerger(moves));
            }

            sb.Append("group by l.pid) t where t.count=" + moves.Length);
            string sql = sb.ToString();
            var dt = DBUtility.ExecuteDataTable(sql);
            return from DataRow dr in dt.Rows select dr[0].ToString().ToInt();
        }

        private static string FindMoveMerger(int[] moves)
        {
            var list = from move in moves select "moveid=" + move;
            return string.Join(" or ", list.ToArray());
        }

        #endregion

        #region override

        protected override string GetTableName()
        {
            return "pbo_pm_learn";
        }

        protected override MoveLearnData DataRowToModel(DataRow dr)
        {
            var data = new MoveLearnData();
            data.Id = dr["id"].ToString().ToInt();
            data.Pid = dr["pid"].ToString().ToInt();
            data.MoveId = dr["moveid"].ToString().ToInt();
            data.Type = (LearnType)(dr["LearnType"].ToString().ToInt());
            data.Info = dr["Info"].ToString();
            data.WithoutTrait = (Trait)(dr["WithoutTraitId"].ToString().ToInt());
            return data;
        }

        #endregion

    }
}